package Example.singleton;

import annoations.Recommend;
import annoations.ThreadSafe;

/**
 * 枚举单例：最安全
 *
 * @author Wang Renlei
 * @Date 2020/7/1
 */
@ThreadSafe
@Recommend
public class SingletonExample7 {

    //私有构造方法
    private SingletonExample7() {

    }

    //静态的工程方法
    public static SingletonExample7 getInstance() {
        return Singleton.INSTANCE.getSingleton();
    }

    private enum Singleton {
        INSTANCE;

        private SingletonExample7 singleton;

        //JVM保证这个方法绝对只调用一次
        Singleton() {
            singleton = new SingletonExample7();
        }

        //提供一个方法给外部获取
        public SingletonExample7 getSingleton() {
            return singleton;
        }
    }

}
